package hust.hbhmzy.udpchat;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import java.net.InetAddress;
import java.net.UnknownHostException;


//登录页面

public class LoginActivity extends Activity {
    static final String LOG_TAG = "UDPchat_Login";
    private String displayName;
    private SharedPreferences pref;//通过SharedPreferences储存用户昵称与是否选择记住昵称
    private SharedPreferences.Editor editor;
    private EditText displayNameText;
    private CheckBox remembername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        displayNameText = findViewById(R.id.editTextDisplayName);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        remembername = findViewById(R.id.login_cb);
        Boolean isRemember = pref.getBoolean("remember_name",false);
        if(isRemember){//如果之前勾选了记住用户名，将保存的昵称放入EditText
            String name = pref.getString("name","");
            displayNameText.setText(name);
            remembername.setChecked(true);
        }
        final Button btnStart = findViewById(R.id.buttonStart);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                displayNameText =findViewById(R.id.editTextDisplayName);
                displayName = displayNameText.getText().toString();
                editor = pref.edit();
                if(remembername.isChecked()){
                    editor.putBoolean("remember_name",true);
                    editor.putString("name",displayName);
                }else editor.clear();
                editor.apply();
                //将昵称与IP传入MainActivity中
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("displayname", displayName);
                bundle.putSerializable("IP",getBroadcastIp());
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });
    }
        //获取自身广播IP
        private InetAddress getBroadcastIp() {
            // Function to return the broadcast address, based on the IP address of the device
            try {

//			WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
                WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                int ipAddress = wifiInfo.getIpAddress();
                String addressString = toBroadcastIp(ipAddress);
                InetAddress broadcastAddress = InetAddress.getByName(addressString);
                return broadcastAddress;
            }
            catch(UnknownHostException e) {

                Log.e(LOG_TAG, "UnknownHostException in getBroadcastIP: " + e);
                return null;
            }

        }

    private String toBroadcastIp(int ip) {
        //以一个常见的ip形式表示
        return (ip & 0xFF) + "." +
                ((ip >> 8) & 0xFF) + "." +
                ((ip >> 16) & 0xFF) + "." +
                "255";
    }


}
